package com.hhxy.ajax;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost方法别调用了");
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet方法被调用了");
        //1、接收请求参数
        String username = request.getParameter("username");
        System.out.println(username);//，乱码处理前，打印测试
        //解决中文乱码
        byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);
        username = new String(bytes,StandardCharsets.UTF_8);
        System.out.println(username);//乱码处理后，打印测试

        //2、这里写调用Service对象的方法查询数据库，这里就直接模拟了
        boolean flag = true;//这里设置为true，无论什么用户名，都是已存在
        //3、响应数据
        response.getWriter().write(flag+"");//又是一个小细节，直接将boolean类型的转换成字符串

    }
}
